<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件委派练习</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            text-decoration: none;
            font-size: 15px;
        }

        .outer {
            width: 400px;
            margin: 100px auto;
            text-align: center;
        }

        table {
            width: 400px;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        td,
        th {
            border: 1px black solid;
            padding: 10px 0;
        }

        form div {
            margin: 10px 0;
        }        
    </style>
    <script>
        document.addEventListener("DOMContentLoaded",()=>{
            const links=document.getElementsByTagName("a")

            document.addEventListener("click",(event)=>{
                if(Array.from(links).includes(event.target)){
                    let flag=confirm(`确认删除${event.target.parentElement.parentElement.firstElementChild.innerText}`)
                    flag&&event.target.parentElement.parentElement.remove()   
                }
            })

            const btn=document.getElementsByTagName("button")[0]
            const tbody=document.getElementsByTagName("tbody")[0]

            btn.addEventListener("click",(event)=>{

            // 创建新的行
            const tr=document.createElement("tr")
            const nameTd=document.createElement("td")
            const emailTd=document.createElement("td")
            const salaryTd=document.createElement("td") 
            // 新建删除单元格 
            const linksTd=document.createElement("td")
            linksTd.appendChild(links[0].cloneNode(true))  //因为所有的删除节点都是一样的，这里就把第一个超链接克隆之后加给新建的删除单元格           
                
            // 获取输入的数据
            const name = document.getElementById("name").value
            const email = document.getElementById("email").value
            const salary = document.getElementById("salary").value

            nameTd.textContent=name
            emailTd.textContent=email
            salaryTd.textContent=salary
            // 将获取到的单元格数据加到新的行里
            tr.appendChild(nameTd)
            tr.appendChild(emailTd)
            tr.appendChild(salaryTd)
            tr.appendChild(linksTd)
            // 将一行td加给整个表格
            tbody.appendChild(tr)
            })

            

        })
    </script>
</head>
<body>
    <div class="outer">
        <table>
            <tbody>
                <tr>
                    <th>姓名</th>
                    <th>邮件</th>
                    <th>薪资</th>
                    <th>操作</th>
                </tr>
                <tr>
                    <td>孙悟空</td>
                    <td>WK.Sun@.com</td>
                    <td>10000</td>
                    <td><a href="javescript:;">删除</a></td>

                </tr>
                <tr>
                    <td>猪八戒</td>
                    <td>WN.Zhu@.com</td>
                    <td>8000</td>
                    <td><a href="javescript:;">删除</a></td>
                </tr>
                <tr>
                    <td>沙和尚</td>
                    <td>WJ.Sha@.com</td>
                    <td>6000</td>
                    <td><a href="javescript:;">删除</a></td>
                </tr>
            </tbody>
        </table>
        <form action="#">
            <div>
                <label for="name">姓名</label>
                <input type="text" id="name">
            </div>
            <div>
                <label for="name">邮件</label>
                <input type="text" id="email">
            </div>
            <div>
                <label for="name">薪资</label>
                <input type="text" id="salary">
            </div>
            <button type="button">添加</button>
        </form>
    </div>    
</body>
</html>